#include <iostream>

#include "lp_lib.h"

#define COLUMNS 2

void Test()
{
	int rows = 3;
	int columns = COLUMNS;

	unsigned char result;

	lprec *lp = 0;

	lp = make_lp( 0, columns );
	if ( lp == 0 )
	{
		std::cout << "can't create LP\n";
		return;
	} else
	{
		std::cout << "LP was created successful\n";
	}

	set_col_name( lp, 1, "x1" );
	set_col_name( lp, 2, "x2" );

	REAL constraint_coefs_matr[][ COLUMNS ] =
	{
		 { 4, -1 },
		 { 2, 1 },
		 { 5, -2 }
	};
	REAL constraints_vector[] = { 8, 10, -2, 0, 0 };
	int constraint_types[] = { LE, LE, GE };

	int * variables_list = new int[ columns ];
	for ( int i = 0; i < columns; i++ )
	{
		variables_list[ i ] = i + 1;
	}

	for ( int i = 0; i < rows; i++ )
	{
		REAL * constraint_coefs = constraint_coefs_matr[ i ];
		result = add_constraintex( lp, columns, constraint_coefs, variables_list, constraint_types[ i ], constraints_vector[ i ] );
		if ( !result )
		{
			std::cout << "can't add_constraintex LP\n";
			return;
		} else
		{
			std::cout << i << " add_constraintex was successful\n";
		}
	}

	for ( int i = 0; i < columns; i++ )
	{
		REAL single_coef = 1;
		int single_variable = i + 1;
		result = add_constraintex( lp, 1, &single_coef, &single_variable, GE, 0 );
	}
	
	set_add_rowmode(lp, FALSE);

	REAL objective_coefs[] = { 1, 1 };
	result = set_obj_fnex( lp, columns, objective_coefs, variables_list );
	if ( !result )
	{
		std::cout << "can't set_obj_fnex LP\n";
		return;
	} else
	{
		std::cout << "set_obj_fnex was successful\n";
	}

	set_maxim( lp );

	write_LP( lp, stdout );
	set_verbose(lp, IMPORTANT);

	result = solve( lp );
	if ( result != OPTIMAL )
	{
		std::cout << "can't solve LP\n";
		return;
	} else
	{
		std::cout << "solve was successful\n";
	}

	REAL answer[ COLUMNS ];
	get_variables( lp, answer );
	for ( int i = 0; i < columns; i++ )
	{
		std::cout << get_col_name( lp, i + 1 ) << " : " << answer[ i ] << "\n";
	}

	delete[] variables_list;
	variables_list = 0;

	delete_lp(lp);
}

int main()
{
	std::cout << "Start...\n";

	Test();

	std::cout << "End.\n";
}
